﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace PM.PMBusiness
{
    /// <summary>
    /// projectapprove 的摘要说明
    /// </summary>
    public class projectapprove
    {
        public projectapprove()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 项目的操作
        /// </summary>
        /// <param name="status">项目需要更改的状态</param>
        /// <param name="id">项目的ID</param>
        /// <returns>返回受影响行数</returns>
        public int projectoperate(int newstatus, int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Bus_Project set status=@status");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                        new SqlParameter("@status", SqlDbType.SmallInt),
                       new SqlParameter("@id", SqlDbType.Int)};
            parameters[0].Value = newstatus;
            parameters[1].Value = id;
            return PM.Code.DAL.DbHelper.ExecuteSql(strSql.ToString(), parameters);//返回受影响的行数
        }
        /// <summary>
        /// 项目的操作
        /// </summary>
        /// <param name="status">项目需要更改的状态</param>
        /// <param name="id">项目的ID</param>
        /// <returns>0：未作修改；1：修改成功；99：错误</returns>
        public int projectoperate(int oldstatus,int newstatus,int id)
        {
            int i = checkstatus(id);
            if (i == 99)
            {
                return 99;//表示发生错误，返回99
            }
            else if (i == oldstatus||i==(oldstatus+20))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update Bus_Project set status=@status");
                strSql.Append(" where id=@id");
                SqlParameter[] parameters = {
                        new SqlParameter("@status", SqlDbType.SmallInt),
                       new SqlParameter("@id", SqlDbType.Int)};
                parameters[0].Value = newstatus;
                parameters[1].Value = id;
                return PM.Code.DAL.DbHelper.ExecuteSql(strSql.ToString(), parameters);//返回受影响的行数
            } 
            else 
            {
                return 0;//返回0，表示未作修改
            }
           
        }
        /// <summary>
        /// 检查项目原状态
        /// </summary>
        /// <param name="id">项目的ID</param>
        /// <returns>项目原状态</returns>
        public int checkstatus(int id)
        {
            string thestatus = "";
            int a = 99;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select status from Bus_Project");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                   new SqlParameter("@id", SqlDbType.Int)};
            parameters[0].Value =id;
            SqlDataReader reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(strSql.ToString(), parameters);
            while(reader.Read())
            {
                try
                {
                    thestatus = reader[0].ToString();
                    a = int.Parse(thestatus);
                }
                catch {}
            }
            reader.Close();
            return a;
        }

        /// <summary>
        /// 获取学院项目允许通过的数量
        /// </summary>
        /// <param name="collegeid">学院ID</param>
        /// <param name="plancode">项目编号</param>
        /// <returns>项目允许通过的数量</returns>
        public int getnumber(string collegeid, string plancode)
        {
            int a = 0;
            string answer = "";
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select number from Bus_NumControl");
            strSql.Append(" where collegeid=@collegeid and plancode=@plancode");
            SqlParameter[] parameters = {
                   new SqlParameter("@collegeid", SqlDbType.NVarChar),
                    new SqlParameter("@plancode",SqlDbType.NVarChar)};
            parameters[0].Value = collegeid;
            parameters[1].Value = plancode;
            SqlDataReader reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(strSql.ToString(), parameters);
            while (reader.Read())
            {
                try
                {
                    answer = reader[0].ToString();
                    a = int.Parse(answer);
                }
                catch { }
            }
            reader.Close();
            return a;
        }
        /// <summary>
        /// 获取指定学院已经通过审批的项目数
        /// </summary>
        /// <param name="collegename">学院名</param>
        /// <param name="plancode">项目编号</param>
        /// <returns>已通过审批的项目数</returns>
        public int getpassednumber(string collegename, string plancode)
        {
            int a = 0;
            string answer = "";
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(id) from Bus_Project");
            strSql.Append(" where pcollege=@collegename and pcode=@pcode and status in (2,3,4,5,11)");
            SqlParameter[] parameters = {
                   new SqlParameter("@collegename", SqlDbType.NVarChar),
                    new SqlParameter("@pcode",SqlDbType.NVarChar),
                     new SqlParameter("@status",SqlDbType.SmallInt)};
            parameters[0].Value = collegename;
            parameters[1].Value = plancode;
            SqlDataReader reader = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(strSql.ToString(), parameters);
            while (reader.Read())
            {
                try
                {
                    answer = reader[0].ToString();
                    a = int.Parse(answer);
                }
                catch { }
            }
            reader.Close();
            return a;
        }
    }
}
